import SearchTop from "../../components/searchTop.vue";
import SearchTitle from "../../components/searchTitle.vue";
import getGame from '@/api/getSearch'
import getAwardDetail from '@/api/award/award-add'
import { jsCallNative } from "../../common/callnative"
import axios from 'axios';
import reportEvent from "../../components/report.js"

export default {
    components: {
        SearchTop: SearchTop,
        SearchTitle: SearchTitle
    },

    data() {
        return {
            inputText:this.$route.query.info , //路由传递过来的值
            gameList: [], //搜索到的游戏数组
            flag: false, //搜索结果
            hotGame: [], // 热门推荐
            gameurl: "http://pitf.vgs.lenovo.com.cn",
            lenovoId:window.sessionStorage.getItem("id"),
            token:this.$route.query.token,
            startTime:+new Date(),
        }
    },
     created(){
       
            //防止页面后退
       //处理键盘事件 禁止后退键（Backspace）密码或单行、多行文本框除外 
        function banBackSpace(e){ 
        var ev = e || window.event;//获取event对象 
        var obj = ev.target || ev.srcElement;//获取事件源 

        var t = obj.type || obj.getAttribute('type');//获取事件源类型 

        //获取作为判断条件的事件类型 
        var vReadOnly = obj.getAttribute('readonly'); 
        var vEnabled = obj.getAttribute('enabled'); 
        //处理null值情况 
        vReadOnly = (vReadOnly == null) ? false : vReadOnly; 
        vEnabled = (vEnabled == null) ? true : vEnabled; 

        //当敲Backspace键时，事件源类型为密码或单行、多行文本的， 
        //并且readonly属性为true或enabled属性为false的，则退格键失效 
        var flag1=(ev.keyCode == 8 && (t=="password" || t=="text" || t=="textarea") 
        && (vReadOnly==true || vEnabled!=true))?true:false; 

        //当敲Backspace键时，事件源类型非密码或单行、多行文本的，则退格键失效 
        var flag2=(ev.keyCode == 8 && t != "password" && t != "text" && t != "textarea") 
        ?true:false; 

        //判断 
        if(flag2){ 
        return false; 
        } 
        if(flag1){ 
        return false; 
        } 
        } 

        //禁止后退键 作用于Firefox、Opera 
        document.onkeypress=banBackSpace; 
        //禁止后退键 作用于IE、Chrome 
        document.onkeydown=banBackSpace; 

    },
    mounted() {
        this.getSearch(this.inputText)
        this.getGameLists()
        if (typeof this.$route.query.errorMessage != "undefined" && this.$route.query.errorMessage != "") {
            this.$toast.center("错误")
        };
         reportEvent.pageIn(this.lenovoId,"104")
    },


    beforeDestroy(){
        reportEvent.pageOut(this.lenovoId,this.startTime,"104")

    },

    watch: {
        $route() {
            
            this.inputText = this.$route.query.info;
            this.getSearch(this.inputText)
        }
    },
    
    methods: {
        // 页面执行请求搜索数据
        getSearch(info) {
             this.inputText = decodeURI(this.$route.query.info)
            console.log(info)
            let that = this;
            that.gameList = [];
            let url = "/search/searchGame?relativeId=48&name=" + info
            this.jqajax(url, { type: "get", dataType: "json" }, function(res) {
               
                // 最新的判断
                if (JSON.stringify(res.datas) !== '[]') { // 有内容
                    
                   
                    for (let j = 0; j < res.datas.length; j++) { // 筛选页游
                       if(res.datas[j].category1=="页游"){
                            that.flag = true
                            that.gameList.push(res.datas[j])
                       }
                    }
                    
                       console.log(that.gameList) 

                       that.gameList.map(function(item,index){

                            for(var i=0;i<item.captureFiles.length;i++){
                                if(item.captureFiles[i].size=="690*434"){
                                    item.bgUrl=item.captureFiles[i].url
                                }
                            }

                           
                       })

                        console.log(that.gameList)
                        console.log(that.flag)
                      

                } else {
                    
                    that.flag = false;
                    that.$toast.top("未搜索到相关游戏")
                }

                console.log( that.gameList)
            })
           let id= window.sessionStorage.getItem("id")
            reportEvent.pageSearch(id)
        },
        //获取YYgame
       getGameYY(cpId,softId,gameName) { // gameId  cpId  softId  游戏名称
            console.log(`/${cpId}/${softId}/${gameName}`)
            
            this.gameStart(cpId,softId,gameName);
        },

        //开始游戏
        gameStart(cpId,softId,gameName) {
             var url = `${this.gameurl}/gameRedirect/loginGame?cp=${cpId}_GAME&game=${softId}&gameName=${gameName}&token=${this.token}`;
             
                window.open(url)
               reportEvent.pageStartGame(this.lenovoId)

        },
        //请求游戏数据
        getGameLists() {
            // 接口地址在serviceUrl里
            getAwardDetail.getAwardDetail(104).then((res) => {
                console.log(res.data)
                // 热门游戏
                this.hotGame = res.data.channels[0].modules[4].elements.slice(0, 4);
                console.log(this.hotGame)
            })
        },
        startGame(gameId) { // 开始游戏页游
            var ts = this
            var _url = "/back/game/get/game/soft/data?softName=yy" + "&ref=LXDT/ZHLX&gameId=" + gameId;
            var ts = this;

            ts.jqajax(_url, { type: "get", dataType: "json" }, function(res) {

                console.log(res.data)
                ts.softBrowser = res.data.softkernel;
                ts.softid = res.data.softId; //game值
                ts.gameStart();

            });
        },


    }

}